layui.config({
    base: "static/layui/module/"
}).extend({
	dynamicCondition: 'dynamicCondition/dynamicCondition',
}).use(['form','layer','jquery','table','laydate','upload','element','dynamicCondition'],function(){
	var form = layui.form,
		layer = layui.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table=layui.table,
        upload = layui.upload,
        laydate =layui.laydate,
        element = layui.element,
        dynamicCondition=layui.dynamicCondition;

	laydate.render({
		elem: '#month'
		,format: 'yyyy-MM' //可任意组合
		,type: 'month'
		,ready: function (date) {
			$(".layui-laydate").on('click', 'ul li', function () {
				$(".layui-laydate").remove();
			});
		}
		,change: function (value) {
			$("#month").val(value);
		}
		,done: function (value){
			var idx;
			$.ajax({
				url: "/manage/report/loadCols",
				type: "post",
				data: {month:value,type:'production'},
				async: true,
				beforeSend: function () {
					idx = top.layer.msg('数据加载中，请稍候', {icon: 16, time: false, shade: 0.1});
				},
				success: function (d) {
					console.log(d);
					table.render({
						elem: '#table'
						,title: '列表'
						,url: '/manage/report/list/'+value+'/production'
						,method: 'post'
						,cols: [d.first,d.second]
						,page: true
						,limit: 15
						,limits: [15,30,40,50,60,70,80,90,100,200]
						,height: 'full-120'
						,toolbar:'#tableToolbar'
						,defaultToolbar: ['filter']
						,text: {
							none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
						}
						,done: function (res, curr, count){
							top.layer.close(idx);
						}
					})
				}
				,error: function () {
					top.layer.close(idx);
					alert("数据加载失败！");
				}
			})
		}
	});

	var dataFields = [
		{field:"sale_order", title:"销售订单", edit:"text"}
		,{field:"customer_code", title:"客户", edit:"select", templet:"#customer"}
		,{field:"work_order", title:"随工单", edit:"text"}
		,{field:"cinvcode", title:"产品", edit:"select", templet:"#product"}
		,{field:"process_code", title:"工序", edit:"select", templet:"#product"}
	];
	var top_dc = undefined;
	var dc = dynamicCondition.create({
    	fields:dataFields
	  	,tableId:"table" 
		,conditionTextId:"#findMsg"
		,type:"complex" 
	  	,requestDataType:"json"
	  	,counts:5
		,queryCallBack:function(requestData){
		}
		,afterOpen: function (_dc) {
			top_dc = _dc;
		}
	});
	
    table.on('toolbar(table)', function(obj){
		switch(obj.event){
			case 'find':
				dc.open();
				break;
			case 'download':
				layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
					var conditionArray = new Array();
					if (top_dc != undefined) {
						for (let i in top_dc.cacheCondition) {
							if (top_dc.cacheCondition[i].conditionValueLeftVal.value != ''
								|| top_dc.cacheCondition[i].conditionValueRightVal.value != '' || top_dc.cacheCondition[i].conditionValueVal.value != '') {
								var condition = {
									field: top_dc.cacheCondition[i].conditionFieldVal
									,title: top_dc.data[i].title
									,type: top_dc.cacheCondition[i].conditionOptionVal
									,right: top_dc.cacheCondition[i].conditionValueRightVal.value
									,left: top_dc.cacheCondition[i].conditionValueLeftVal.value
									,value: top_dc.cacheCondition[i].conditionValueVal.value
								}
								conditionArray.push(condition);
							}
						}
					}
					window.location.href = "/manage/report/download/"+$("#month").val()+"/production?condition=" + encodeURI(JSON.stringify(conditionArray));
					layer.close(index);
				});
				break;
		};
	});
})
